package com.wandian.ui.controls
{
	import flash.display.*;
	import flash.events.*;
	
	public class AnimatedButton extends MovieClip
	{
		private var fButtonStatesAnimations_bsa_arr:Array;
		private var fEnabled_bl:Boolean;
		private var fIdleState_bl:Boolean;
		private var fDefferedAnimationsStack_bsa_arr:Array;
		private var fActiveArea_sb:SimpleButton;
		private var fMousePressedOnTheButtonAndNotReleased_bl:Boolean;
		private var fAnimationContainer_mc:MovieClip;
		public static const EVENT_IDLE_ANIMATION_STARTED:String = "onAnimatedButtonIdleAnimationStarted";
		public static const EVENT_DISABLE_ANIMATION_STARTED:String = "onAnimatedButtonDisableAnimationStarted";
		public static const EVENT_IDLE_ANIMATION_PERIOD_COMPLETED:String = "onAnimatedButtonIdleAnimationPeriodCompleted";
		public static const EVENT_OVER_ANIMATION_STARTED:String = "onAnimatedButtonOverAnimationStarted";
		public static const EVENT_OVER_ANIMATION_COMPLETED:String = "onAnimatedButtonOverAnimationCompleted";
		public static const EVENT_INTRO_ANIMATION_COMPLETED:String = "onAnimatedButtonIntroAnimationCompleted";
		public static const EVENT_DISABLE_ANIMATION_COMPLETED:String = "onAnimatedButtonDisableAnimationCompleted";
		public static const EVENT_BUTTON_CLICKED:String = "onButtonClicked";
		
		public function AnimatedButton(param1:MovieClip, param2:MovieClip, param3:MovieClip, param4:MovieClip, param5:MovieClip, param6:MovieClip)
		{
			var lPlaceholder_mc:MovieClip;
			var aIntroAnimation_mc:* = param1;
			var aIdleAnimaion_mc:* = param2;
			var aRollOverAnimation_mc:* = param3;
			var aDownAnimation_mc:* = param4;
			var aUpAnimation_mc:* = param5;
			var aDisableAnimation_mc:* = param6;
			this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_INTRO, aIntroAnimation_mc);
			this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_IDLE, aIdleAnimaion_mc);
			this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_OVER, aRollOverAnimation_mc);
			this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DOWN, aDownAnimation_mc);
			this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_UP, aUpAnimation_mc);
			this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DISABLE, aDisableAnimation_mc);
			this.fAnimationContainer_mc = this["animationContainer_mc"];
			this.fActiveArea_sb = this["_btn"];
			try
			{
				lPlaceholder_mc = this["placeholder_mc"];
				this.removeChild(lPlaceholder_mc);
			}
			catch (aError_err:Error)
			{
			}
			this.updateState(false);
			this.fEnabled_bl = true;
			this.mouseEnabled = false;
			this.animationContainer.mouseEnabled = false;
			this.disable();
			return;
		}// end function
		
		protected function onMouseOver(event:Event) : void
		{
			this.updateState(false);
			if (this.fMousePressedOnTheButtonAndNotReleased_bl)
			{
				this.processRequiredAnimation(this.downAnimation);
			}
			else
			{
				this.processRequiredAnimation(this.overAnimation);
			}
			return;
		}// end function
		
		protected function cancelDefferedAnimations() : void
		{
			this.fDefferedAnimationsStack_bsa_arr = new Array();
			return;
		}// end function
		
		protected function get downAnimation() : ButtonStateAnimation
		{
			return this.getButtonStateAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DOWN);
		}// end function
		
		protected function get idleAnimation() : ButtonStateAnimation
		{
			return this.getButtonStateAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_IDLE);
		}// end function
		
		protected function get overAnimation() : ButtonStateAnimation
		{
			return this.getButtonStateAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_OVER);
		}// end function
		
		protected function get currentlyPlayingAnimation() : ButtonStateAnimation
		{
			var _loc_1:int = 0;
			_loc_1 = 0;
			while (_loc_1 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).inUse && ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).animationInProgress)
				{
					return ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]);
				}
				_loc_1++;
			}
			return null;
		}// end function
		
		protected function onButtonClicked(event:Event) : void
		{
			this.fMousePressedOnTheButtonAndNotReleased_bl = false;
			this.processRequiredAnimation(this.upAnimation);
			this.dispatchEvent(new Event(EVENT_BUTTON_CLICKED));
			return;
		}// end function
		
		public function get animationInProgress() : Boolean
		{
			var _loc_1:int = 0;
			_loc_1 = 0;
			while (_loc_1 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).inUse && ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).animationInProgress)
				{
					return true;
				}
				_loc_1++;
			}
			return false;
		}// end function
		
		protected function get upAnimation() : ButtonStateAnimation
		{
			return this.getButtonStateAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_UP);
		}// end function
		
		public function get idleState() : Boolean
		{
			return this.fIdleState_bl;
		}// end function
		
		public function get allowIntroAnimation() : Boolean
		{
			return this.introAnimation.allowAnimation;
		}// end function
		
		public function addAlternativeIntroAnimation(param1:MovieClip) : int
		{
			return this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_INTRO, param1);
		}// end function
		
		protected function getButtonStateAnimation(param1:String) : ButtonStateAnimation
		{
			var _loc_2:int = 0;
			_loc_2 = 0;
			while (_loc_2 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).animationType == param1 && ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).inUse)
				{
					return ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]);
				}
				_loc_2++;
			}
			return null;
		}// end function
		
		public function set alternativeIntroAnimationInUseId(param1:int) : void
		{
			this.useAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_INTRO, param1);
			return;
		}// end function
		
		override public function get enabled() : Boolean
		{
			return this.fEnabled_bl;
		}// end function
		
		protected function updateState(param1:Boolean) : void
		{
			this.fIdleState_bl = param1;
			return;
		}// end function
		
		protected function get thereAreDefferedAnimations() : Boolean
		{
			return this.defferedAnimations.length > 0;
		}// end function
		
		public function getAlternativeAnimationInUseId(param1:String) : int
		{
			var _loc_2:int = 0;
			_loc_2 = 0;
			while (_loc_2 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).animationType == param1 && ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).inUse)
				{
					return ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).alternativeId;
				}
				_loc_2++;
			}
			throw Error("Internal error");
		}// end function
		
		protected function onMouseUpOutsideTheButton(event:Event) : void
		{
			this.fMousePressedOnTheButtonAndNotReleased_bl = false;
			return;
		}// end function
		
		protected function get animationContainer() : MovieClip
		{
			return this.fAnimationContainer_mc;
		}// end function
		
		public function get allowIdleAnimation() : Boolean
		{
			return this.idleAnimation.allowAnimation;
		}// end function
		
		protected function get buttonStatesAnimations() : Array
		{
			if (!this.fButtonStatesAnimations_bsa_arr)
			{
				this.fButtonStatesAnimations_bsa_arr = new Array();
			}
			return this.fButtonStatesAnimations_bsa_arr;
		}// end function
		
		protected function stopToPlayAnimations(param1:ButtonStateAnimation) : void
		{
			var _loc_2:* = undefined;
			_loc_2 = 0;
			while (_loc_2 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).inUse && ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).animationInProgress)
				{
					ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).stopToPlayAnimation();
				}
				_loc_2 = _loc_2 + 1;
			}
			return;
		}// end function
		
		protected function planAnimationPlaying(param1) : void
		{
			var _loc_2:int = 0;
			var _loc_3:int = 0;
			if (this.defferedAnimations.length == 0)
			{
				this.defferedAnimations.push(param1);
				return;
			}
			_loc_2 = this.defferedAnimations.length;
			_loc_3 = 0;
			while (_loc_3 < _loc_2)
			{
				
				if (param1.canInterrupt(ButtonStateAnimation(this.defferedAnimations[(this.defferedAnimations.length - 1)]).animationType) || !param1.canInterrupt(ButtonStateAnimation(this.defferedAnimations[(this.defferedAnimations.length - 1)]).animationType) && ButtonStateAnimation(this.defferedAnimations[(this.defferedAnimations.length - 1)]).animationType == param1.animationType)
				{
					this.defferedAnimations.pop();
				}
				else
				{
					break;
				}
				_loc_3++;
			}
			if (this.defferedAnimations.length == 0 && this.animationInProgress && this.currentlyPlayingAnimation.animationType == param1.animationType)
			{
				return;
			}
			this.defferedAnimations.push(param1);
			return;
		}// end function
		
		protected function onButtonStateAnimationCompleted(event:Event)
		{
			var _loc_2:ButtonStateAnimation = null;
			_loc_2 = ButtonStateAnimation(event.target);
			_loc_2.removeEventListener(Animation.EVENT_ANIMATION_COMPLETED, this.onButtonStateAnimationCompleted);
			switch(_loc_2.animationType)
			{
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_INTRO:
				{
					this.updateState(true);
					this.dispatchEvent(new Event(EVENT_INTRO_ANIMATION_COMPLETED));
					this.processRequiredAnimation(this.idleAnimation);
					if (this.enabled)
					{
						this.startToReceiveUserInput();
					}
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_IDLE:
				{
					this.dispatchEvent(new Event(EVENT_IDLE_ANIMATION_PERIOD_COMPLETED));
					this.processRequiredAnimation(this.idleAnimation);
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DISABLE:
				{
					this.updateState(false);
					this.dispatchEvent(new Event(EVENT_DISABLE_ANIMATION_COMPLETED));
					this.cancelDefferedAnimations();
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DOWN:
				{
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_UP:
				{
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_OVER:
				{
					this.dispatchEvent(new Event(EVENT_OVER_ANIMATION_COMPLETED));
					break;
				}
				default:
				{
					break;
				}
			}
			this.processNextPlannedAnimation();
			return;
		}// end function
		
		protected function onMouseOut(event:Event) : void
		{
			this.updateState(true);
			if (this.fMousePressedOnTheButtonAndNotReleased_bl)
			{
				this.processRequiredAnimation(this.upAnimation);
			}
			this.processRequiredAnimation(this.idleAnimation);
			return;
		}// end function
		
		public function set allowIntroAnimation(param1:Boolean) : void
		{
			this.introAnimation.allowAnimation = param1;
			return;
		}// end function
		
		protected function get introAnimation() : ButtonStateAnimation
		{
			return this.getButtonStateAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_INTRO);
		}// end function
		
		override public function set enabled(param1:Boolean) : void
		{
			if (this.enabled == param1)
			{
				return;
			}
			if (param1)
			{
				this.enable();
			}
			else
			{
				this.disable();
			}
			return;
		}// end function
		
		protected function enable() : void
		{
			if (this.enabled)
			{
				return;
			}
			this.fEnabled_bl = true;
			this.processRequiredAnimation(this.introAnimation);
			return;
		}// end function
		
		protected function startToPlayAnimation(param1:ButtonStateAnimation) : void
		{
			this.detachButtonStateAnimations();
			param1.addEventListener(Animation.EVENT_ANIMATION_STARTED, this.onAnimationPlayingStarted);
			param1.addEventListener(Animation.EVENT_ANIMATION_COMPLETED, this.onButtonStateAnimationCompleted);
			param1.startToPlayAnimation();
			return;
		}// end function
		
		protected function detachButtonStateAnimations() : void
		{
			var _loc_1:* = undefined;
			_loc_1 = 0;
			while (_loc_1 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).inUse && ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).animationInProgress)
				{
					ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).removeEventListener(Animation.EVENT_ANIMATION_STARTED, this.onAnimationPlayingStarted);
					ButtonStateAnimation(this.buttonStatesAnimations[_loc_1]).removeEventListener(Animation.EVENT_ANIMATION_COMPLETED, this.onButtonStateAnimationCompleted);
				}
				_loc_1 = _loc_1 + 1;
			}
			return;
		}// end function
		
		protected function getAlternativeButtonStateAnimationToUse(param1:String, param2:int)
		{
			var _loc_3:int = 0;
			_loc_3 = 0;
			while (_loc_3 < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_3]).animationType == param1 && ButtonStateAnimation(this.buttonStatesAnimations[_loc_3]).alternativeId == param2)
				{
					return ButtonStateAnimation(this.buttonStatesAnimations[_loc_3]);
				}
				_loc_3++;
			}
			throw Error("Alternative button state animation does not exist");
		}// end function
		
		public function addAlternativeAnimation(param1:String, param2:MovieClip) : int
		{
			var lButtonStateAnimation_bsa:ButtonStateAnimation;
			var aAnimationType_str:* = param1;
			var aAnimation_mc:* = param2;
			try
			{
				lButtonStateAnimation_bsa = ButtonStateAnimation(aAnimation_mc);
			}
			catch (aError_err:Error)
			{
				lButtonStateAnimation_bsa = new ButtonStateAnimation(aAnimation_mc);
			}
			lButtonStateAnimation_bsa.animationType = aAnimationType_str;
			lButtonStateAnimation_bsa.alternativeId = this.generateAlternativeAnimationId(lButtonStateAnimation_bsa.animationType);
			if (lButtonStateAnimation_bsa.alternativeId == 0)
			{
				lButtonStateAnimation_bsa.inUse = true;
			}
			else
			{
				lButtonStateAnimation_bsa.inUse = false;
			}
			this.buttonStatesAnimations.push(lButtonStateAnimation_bsa);
			return lButtonStateAnimation_bsa.alternativeId;
		}// end function
		
		protected function get defferedAnimations() : Array
		{
			if (!this.fDefferedAnimationsStack_bsa_arr)
			{
				this.fDefferedAnimationsStack_bsa_arr = new Array();
			}
			return this.fDefferedAnimationsStack_bsa_arr;
		}// end function
		
		protected function onButtonStateAnimationStarted(event:Event) : void
		{
			switch(ButtonStateAnimation(event.target).animationType)
			{
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_OVER:
				{
					this.onOverAnimationStarted(event);
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_IDLE:
				{
					this.dispatchEvent(new Event(EVENT_IDLE_ANIMATION_STARTED));
					break;
				}
				case ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DISABLE:
				{
					this.dispatchEvent(new Event(EVENT_DISABLE_ANIMATION_STARTED));
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}// end function
		
		public function get alternativeIntroAnimationInUseId() : int
		{
			return this.getAlternativeAnimationInUseId(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_INTRO);
		}// end function
		
		public function set allowIdleAnimation(param1:Boolean) : void
		{
			this.idleAnimation.allowAnimation = param1;
			if (param1 && this.idleState)
			{
				this.processRequiredAnimation(this.idleAnimation);
			}
			return;
		}// end function
		
		protected function generateAlternativeAnimationId(param1:String) : int
		{
			var _loc_2:int = 0;
			_loc_2 = this.buttonStatesAnimations.length - 1;
			while (_loc_2 >= 0)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).animationType == param1)
				{
					return (ButtonStateAnimation(this.buttonStatesAnimations[_loc_2]).alternativeId + 1);
				}
				_loc_2 = _loc_2 - 1;
			}
			return 0;
		}// end function
		
		public function set alternativeDisableAnimationInUseId(param1:int) : void
		{
			this.useAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DISABLE, param1);
			return;
		}// end function
		
		protected function get disalbeAnimation() : ButtonStateAnimation
		{
			return this.getButtonStateAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DISABLE);
		}// end function
		
		public function set allowOverAnimation(param1:Boolean) : void
		{
			this.overAnimation.allowAnimation = param1;
			return;
		}// end function
		
		protected function get activeArea() : SimpleButton
		{
			return this.fActiveArea_sb;
		}// end function
		
		public function allowInterruption(param1:String, param2:String, param3:Boolean) : void
		{
			this.getButtonStateAnimation(param1).canInterrupt(param2, true, param3);
			return;
		}// end function
		
		public function useAlternativeAnimation(param1:String, param2:int) : void
		{
			var _loc_3:ButtonStateAnimation = null;
			var _loc_4:ButtonStateAnimation = null;
			if (this.getAlternativeAnimationInUseId(param1) == param2)
			{
				return;
			}
			_loc_3 = this.getAlternativeButtonStateAnimationToUse(param1, param2);
			_loc_4 = this.getButtonStateAnimation(param1);
			_loc_4.inUse = false;
			_loc_3.inUse = true;
			return;
		}// end function
		
		protected function processNextPlannedAnimation() : void
		{
			var _loc_1:ButtonStateAnimation = null;
			if (this.defferedAnimations.length == 0)
			{
				return;
			}
			if (this.currentlyPlayingAnimation)
			{
			}
			_loc_1 = this.defferedAnimations[0];
			if (this.animationInProgress && _loc_1.canInterrupt(this.currentlyPlayingAnimation.animationType) || !this.animationInProgress)
			{
				this.defferedAnimations.splice(0, 1);
				if (!_loc_1.allowAnimation)
				{
					this.processNextPlannedAnimation();
				}
				else
				{
					this.startToPlayAnimation(_loc_1);
				}
			}
			return;
		}// end function
		
		protected function onButtonDown(event:Event) : void
		{
			this.fMousePressedOnTheButtonAndNotReleased_bl = true;
			this.processRequiredAnimation(this.downAnimation);
			return;
		}// end function
		
		public function set allowDisableAnimation(param1:Boolean) : void
		{
			this.disalbeAnimation.allowAnimation = param1;
			return;
		}// end function
		
		protected function stopToReceiveUserInput() : void
		{
			this.activeArea.useHandCursor = false;
			this.activeArea.mouseEnabled = false;
			this.activeArea.enabled = false;
			this.activeArea.removeEventListener(MouseEvent.MOUSE_DOWN, this.onButtonDown);
			this.activeArea.removeEventListener(MouseEvent.CLICK, this.onButtonClicked);
			if (this.activeArea.stage)
			{
				this.activeArea.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpOutsideTheButton);
			}
			this.activeArea.removeEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
			this.activeArea.removeEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);
			return;
		}// end function
		
		protected function onAnimationPlayingStarted(event:Event) : void
		{
			var lAnimation_bsa:ButtonStateAnimation;
			var i:*;
			var aEvent_evnt:* = event;
			lAnimation_bsa = ButtonStateAnimation(aEvent_evnt.target);
			lAnimation_bsa.removeEventListener(Animation.EVENT_ANIMATION_STARTED, this.onAnimationPlayingStarted);
			this.onButtonStateAnimationStarted(aEvent_evnt);
			this.animationContainer.addChild(lAnimation_bsa.animationShell);
			i;
			while (i < this.buttonStatesAnimations.length)
			{
				
				if (ButtonStateAnimation(this.buttonStatesAnimations[i]).animationShell == lAnimation_bsa.animationShell)
				{
				}
				else
				{
					try
					{
						this.animationContainer.removeChild(ButtonStateAnimation(this.buttonStatesAnimations[i]).animationShell);
					}
					catch (aError_err:Error)
					{
					}
				}
				i = (i + 1);
			}
			return;
		}// end function
		
		protected function onOverAnimationStarted(event:Event) : void
		{
			this.dispatchEvent(new Event(EVENT_OVER_ANIMATION_STARTED));
			return;
		}// end function
		
		protected function startToReceiveUserInput() : void
		{
			this.activeArea.addEventListener(MouseEvent.MOUSE_DOWN, this.onButtonDown);
			this.activeArea.addEventListener(MouseEvent.CLICK, this.onButtonClicked);
			this.activeArea.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
			this.activeArea.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);
			if (this.activeArea.stage)
			{
				this.activeArea.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpOutsideTheButton);
			}
			this.activeArea.useHandCursor = true;
			this.activeArea.mouseEnabled = true;
			this.activeArea.enabled = true;
			return;
		}// end function
		
		protected function addDefferedAnimation(param1:ButtonStateAnimation) : void
		{
			this.defferedAnimations.push(param1);
			return;
		}// end function
		
		public function addAlternativeDisableAnimation(param1:MovieClip) : int
		{
			return this.addAlternativeAnimation(ButtonStateAnimation.BUTTON_ANIMATION_TYPE_DISABLE, param1);
		}// end function
		
		protected function disable() : void
		{
			if (!this.enabled)
			{
				return;
			}
			this.fEnabled_bl = false;
			this.stopToReceiveUserInput();
			this.processRequiredAnimation(this.disalbeAnimation);
			return;
		}// end function
		
		protected function processRequiredAnimation(param1:ButtonStateAnimation) : void
		{
			this.planAnimationPlaying(param1);
			this.processNextPlannedAnimation();
			return;
		}// end function
		
	}
}
